﻿
using HBase;
using hcollection;
using hconnect;
using System.Collections.Generic;
namespace hCommand {
    public abstract class DeviceCode :ZtTask {
        public abstract SocketZtTaskConnect getTaskConnect(BaseTask taskModel);
        protected ICoreTaskServer coreTaskServer = WMSDalFactray.getDal<ICoreTaskServer>();
        protected abstract int DeviceId();
        public virtual bool beforeSend(List<BaseTask> taskModels) {
            foreach( var taskModel in taskModels ) {
                if( null == taskModel )
                    continue;
                int count = coreTaskServer.beginTask(taskModel);
                if( count != 1 ) {
                    fileAll("{0}update_param_error " ,taskModel.taskNo);
                    return false;
                }
            }
            return true;

        }
        public bool DoOutAndShiftTask(List<BaseTask> taskModels) {

            //修改wms任务表为下发完成
            if( !beforeSend(taskModels) ) {
                return false;
            }

            return send(taskModels);
        }


        public virtual bool send(List<BaseTask> taskModels) {
            foreach( var taskModel in taskModels ) {

                SocketZtTaskConnect conn = getTaskConnect(taskModel);
                if( conn.sendTask(taskModel ,getBegin()) ) {
                    oneTimeSleep = StatusCache.signalLife;
                    continue;
                } else {
                    fileAll("{0} at {1}Send_error" ,taskModel.taskNo ,taskModel.src);
                    continue;
                }
            }
            return true;
        }
        public virtual int getBegin() {
            return 0;
        }
    }
}
